package org.dromara.merchants.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.merchants.domain.vo.TaAssociationVo;
import org.dromara.merchants.domain.bo.TaAssociationBo;
import org.dromara.merchants.service.ITaAssociationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 关联
 * 前端访问路由地址为:/merchants/association
 *
 * @author xiyin321
 * @date 2024-07-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/association")
public class TaAssociationController extends BaseController {

    private final ITaAssociationService taAssociationService;

    /**
     * 查询关联列表
     */
    @SaCheckPermission("merchants:association:list")
    @GetMapping("/list")
    public TableDataInfo<TaAssociationVo> list(TaAssociationBo bo, PageQuery pageQuery) {
        return taAssociationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出关联列表
     */
    @SaCheckPermission("merchants:association:export")
    @Log(title = "关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaAssociationBo bo, HttpServletResponse response) {
        List<TaAssociationVo> list = taAssociationService.queryList(bo);
        ExcelUtil.exportExcel(list, "关联", TaAssociationVo.class, response);
    }

    /**
     * 获取关联详细信息
     *
     * @param associationId 主键
     */
    @SaCheckPermission("merchants:association:query")
    @GetMapping("/{associationId}")
    public R<TaAssociationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long associationId) {
        return R.ok(taAssociationService.queryById(associationId));
    }

    /**
     * 新增关联
     */
    @SaCheckPermission("merchants:association:add")
    @Log(title = "关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaAssociationBo bo) {
        return toAjax(taAssociationService.insertByBo(bo));
    }

    /**
     * 修改关联
     */
    @SaCheckPermission("merchants:association:edit")
    @Log(title = "关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaAssociationBo bo) {
        return toAjax(taAssociationService.updateByBo(bo));
    }

    /**
     * 删除关联
     *
     * @param associationIds 主键串
     */
    @SaCheckPermission("merchants:association:remove")
    @Log(title = "关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{associationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] associationIds) {
        return toAjax(taAssociationService.deleteWithValidByIds(List.of(associationIds), true));
    }
}
